define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zone/zone/index' + location.search,
                    add_url: 'zone/zone/add',
                    edit_url: 'zone/zone/edit',
                    del_url: 'zone/zone/del',
                    multi_url: 'zone/zone/multi',
                    table: 'zone',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showExport: false,
                commonSearch: false,
                exportTypes:['excel'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false,visible: false},
                        {field: 'type', title: __('Type'),operate: false,visible: false},
                        {field: 'name', title: __('Name'),operate: false,visible: false},
                        {field: 'host', title: __('Host'),operate: false,visible: false},
                        {field: 'username', title: __('Username'),visible: false,operate: false},
                        {field: 'password', title: __('Password'),visible: false,operate: false},
                        {field: 'port', title: __('Port'),operate: false,visible: false},
                        {field: 'sort', title: __('Sort'),operate: false,visible: false},
                        {field: 'dbname', title: __('Dbname'),searchList: {
                                "web":"web",
                                "tlbbdb":"tlbbdb",
                            },formatter:function (value,row,index) {
                                if (value == 'web'){return '<span class="label bg-orange">web</span>';}
                                if (value == 'tlbbdb'){return '<span class="label bg-purple">tlbbdb</span>';}
                            }},
                        {field: 'status', title: __('Status'),searchList: {
                                "0":"禁用",
                                "1":"正常",
                            },formatter:function (value,row,index) {
                                if (value ===0){return '<span class="label bg-red">禁用</span>';}
                                if (value ===1){return '<span class="label bg-green">正常</span>';}
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible: false},
                        {
                            field: 'buttons',
                            title: '测试连接状况',
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: '测试连接',
                                    title: '测试连接',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-play',
                                    url: 'zone/zone/startTest',
                                    confirm: '确定测试',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});